
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package client;

import com.sun.org.apache.bcel.internal.generic.GOTO;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import protocol.Message;
import protocol.MetaData;
import protocol.Protocol;
import sun.misc.Cleaner;
import sun.print.resources.serviceui;
import utility.Utility;



/**
 *
 * @author Acer
 */ 
public class Client {
    //Application and default folder name
    public final static String APPS_NAME = "8box";
    
    public final static String DELETED_FILE_NAME = "deleted_file.txt";
    
    //Socket dan I/O yang diperlukan
    public Socket clientSocket = null;
    BufferedOutputStream bosSocket = null;
    BufferedInputStream bisSocket = null;
    
    //HOST dan PORT
//    public final static String HOST = "masphei-PC";
//    public final static String HOST = "167.205.35.124";
    public final static String HOST = "AS4810T";
//    public final static String HOST = "localhost";
    public final static int PORT = 4444;
    
    //Username dan password dari user
    private String username;
    private String password;
    
    public void setUsername(String value){
        username = value;
    }
    
    public String getUsername(){
        return username;
    }
    
    public void setPassword(String value){
        password = value;
    }
    
    public String getPassword(){
        return password;
    }
    
    //Permasalahan Login
    public boolean isStillLogin = false;
    public void Login(){
        InitClientSocket();
        InitOutIn();
        System.out.println("Inisialisasi true");
    }
    
    //Permasalahan Koneksi masih ada
    public boolean isStillConnect = false;
    public void Connect(){
        isStillConnect = true;
    }
    
    //Long untuk waktu sinkronisasi request oleh thread syncron
    public final static long SYNCHRONIZE_TIME = 5000;
    
    //Long untuk waktu periodik pengecekan file yang dideleted
    public final static long SYNCHRONIZE_TIME_DELETED = SYNCHRONIZE_TIME/2;
    
    //Path awal file yang akan disimpan di eksternal file, default di D:\\8box
    public String clientDirectory = "D:\\8box\\client\\8box";
    public void setClientDirectory(String value){
        clientDirectory = value;
    }
    public String getClientDirectory(){
        return clientDirectory;
    }
    
    //Client working directory meta data
    ArrayList<MetaData> arrMetaData;
    
    //Untuk mengecek apakah masih connect dengan server
//    public boolean isStillConnect = false;
    
    
    
    public void CreateNewClientDirectory(){
        clientDirectory += APPS_NAME;
        if (utility.Utility.CreateNewDirectory(clientDirectory)){
            System.out.println("SUCCESS create new directory");
        }else{
            System.out.println("FAILED create new directory");
        }
    }
    
    public boolean isApplicationStart = false;
    
    public Client(){
        isApplicationStart = true;
        synchronizeThread.start();
    }
    
    public void InitClientSocket(){
        
        while (!isStillConnect){
            try {
                clientSocket = new Socket(HOST, PORT);
                isStillConnect = true;
            } catch (UnknownHostException ex) {
                System.err.println("Unknown host : " + HOST);
            } catch (IOException ex) {
                System.err.println("Couldn't get I/O for the connection to: " + HOST);
            }
        }
        
    }
    
    
    
    public void InitOutIn(){
        try {
            bosSocket = new BufferedOutputStream(clientSocket.getOutputStream());
            bisSocket = new BufferedInputStream(clientSocket.getInputStream());
        } catch (IOException ex) {
            System.err.println("Can not out and in");
            System.exit(-1);
        }
    }
    
    public void Logout() throws IOException, UnsupportedEncodingException, NoSuchAlgorithmException{
//        synchronizeThread.interrupt();
        SendLogOutToServer();
        bosSocket.close();
        bisSocket.close();
        clientSocket.close();
        isStillConnect = false;
        isStillLogin = false;
    }
    
    public void Login(String username,String password) throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException{
        Login();
        String[] parameter = new String[2];
        parameter[0] = username;
        parameter[1] = password;
        Message loginMessage = new Message(Message.MESSAGE_LOGIN, parameter);
        while (!loginMessage.sendMessage(clientSocket,bosSocket, loginMessage)){
            System.out.println("Bangun koneksi ke server");
        }
//        isStillLogin = true;
    }
    
    public void SendRequestMetaData() throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException{
        //Kirim dulu message metadata ke server
        System.out.println("Kirim request synchron");
        Message metadataRequestMessage = new Message(Message.MESSAGE_METADATA, null);
        metadataRequestMessage.sendMessage(clientSocket, bosSocket, metadataRequestMessage);
    }
    
    public void SendAckToServer() throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException{
        Message ackMessage = new Message(Message.MESSAGE_ACK, null);
        ackMessage.sendMessage(clientSocket, bosSocket, ackMessage);
    }
    
    public void SendDeleteToServer() throws IOException, UnsupportedEncodingException, NoSuchAlgorithmException {
        Message deleteFileListRequest = new Message(Message.MESSAGE_DELETE, null);
        deleteFileListRequest.sendMessage(clientSocket, bosSocket, deleteFileListRequest);
    }
    
    public void SendLogOutToServer() throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException{
        Message logOutMessage = new Message(Message.MESSAGE_LOGOUT, null);
        logOutMessage.sendMessage(clientSocket, bosSocket, logOutMessage);
    }
    
    public void MessageUpload(long length, byte type, String fileRelative) throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException {
        String[] param = new String[3];
        param[0] = Long.toString(length);
        param[1] = Byte.toString(type);
        param[2] = ("\"" + fileRelative + "\"");
        Message uploadMessage = new Message(Message.MESSAGE_UPLOAD, param);
        uploadMessage.sendMessage(clientSocket, bosSocket, uploadMessage);
        //Downloading(length, clientDirectory+"\\"+fileRelative);
    }
    public void Uploading(int port, String fileRelative){
//        System.out.println("download respon port:" + port);
//        System.out.println("uploading "+clientDirectory + "\\" +fileRelative);
        try {
            Socket uploadSocket = new Socket(HOST,port);
            OutputStream os = uploadSocket.getOutputStream();
            BufferedOutputStream out = new BufferedOutputStream(os, 1024);
            FileInputStream in = new FileInputStream(clientDirectory + "\\" +fileRelative);
            byte[] buf = new byte[1024];
            int i = 0;
            int bytecount = 1024;
            while ((i = in.read(buf, 0, 1024)) != -1) {
//              System.out.println("bytecount=" + bytecount);
              bytecount = bytecount + 1024;
              
              try{
                out.write(buf, 0, i);
                out.flush();
              }catch(SocketException ex){
                  System.out.println("out.write() failed");
                  break;
              }
              
            }
            uploadSocket.shutdownOutput(); /* important */
//            System.out.println("END");
            in.close();
            uploadSocket.close();
        } catch (IOException ex) {
            System.out.println("io exception");
        }
    }
    public void DownloadRequest(long length, String fileRelative) throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException {
        String[] param = new String[2];
        param[0] = Long.toString(length);
        param[1] = ("\"" + fileRelative + "\"");
        Message downloadRequestMessage = new Message(Message.MESSAGE_DOWNLOAD_REQUEST, param);
        downloadRequestMessage.sendMessage(clientSocket, bosSocket, downloadRequestMessage);
        //Downloading(length, clientDirectory+"\\"+fileRelative);
    }
    public void DownloadRequestNewPort(int port, long length, String fileRelative) throws UnsupportedEncodingException, NoSuchAlgorithmException, IOException {
        Socket sending = new Socket(HOST, port);
        String[] param = new String[2];
        param[0] = Long.toString(length);
        param[1] = ("\"" + fileRelative + "\"");
        Message downloadRequestMessage = new Message(Message.MESSAGE_DOWNLOAD_REQUEST, param);
        System.out.println("kirim ke port "+port);
        downloadRequestMessage.sendMessage(sending, new BufferedOutputStream(sending.getOutputStream()), downloadRequestMessage);
        System.out.println("port telah terkirim");
        Downloading(sending, length, clientDirectory+"\\"+fileRelative);
    }
    public void Downloading(Socket newSocket, long length, String pathDestination) throws FileNotFoundException, IOException {
        byte[] b = new byte[1024];
        int len = 0;
        long bytcount = 1024;
        FileOutputStream inFile = new FileOutputStream(pathDestination);
        InputStream is = newSocket.getInputStream();
        BufferedInputStream in2 = new BufferedInputStream(is, 1024);
        while ((len = in2.read(b, 0, 1024)) != -1) {
              System.out.println("byte count=" + bytcount);
              bytcount = bytcount + 1024;
              inFile.write(b, 0, len);
        }
        inFile.close();
//        is.close();
        System.out.println("Bytes Writen : " + bytcount+" in "+pathDestination);
        //bosSocket.write(Byte.parseByte("TEEST"));
        System.out.println(bytcount+":"+length);
        if(bytcount<length) {
            //pathDestination = pathDestination.replace("/", "\\");
            //System.out.println("file gagal "+pathDestination);
            //Utility.DeleteFile(pathDestination);

//            File file = new File(pathDestination);
//            if (file.exists()) {
//                if(file.delete())
//                System.out.println("file deleted");
//                else 
//                    System.out.println("file failed to delete");
//            } else {
//                System.err.println(
//                    "I cannot find '" + file + "' ('" + file.getAbsolutePath() + "')");
//            }

        }else{
            System.out.println("file has been downloaded");
        }
  }
    
    
    public ArrayList<MetaData> CreateClientMetadata(){
        ArrayList<MetaData> arrMetaData = utility.Utility.getMetaDataFromFolder(clientDirectory);
        return arrMetaData;
    }
    
    public ArrayList<MetaData> beforeAllFileMetaData = null;
    public ArrayList<MetaData> allDeletedFileMetaData = new ArrayList<MetaData>();
//    public 
    
    public ArrayList<MetaData> copyDeepArrListMetaData(ArrayList<MetaData> value){
        ArrayList<MetaData> retval = new ArrayList<MetaData>();
        for(int i=0;i<value.size();++i){
            retval.add(value.get(i));
        }
        return retval;
    }
    
    public void InsertRewriteOneMetaData(MetaData value,ArrayList<MetaData> arrMetaData){
        int i;
        boolean fileNotFound = true;
        for(i=0;i<arrMetaData.size();++i){
            if (arrMetaData.get(i).getFilePath().equals(value.getFilePath())){
                fileNotFound = false;
                arrMetaData.set(i, new MetaData(value));
            }
        }
        if (fileNotFound){
            arrMetaData.add(new MetaData(value));
        }
    }
    
    public void CheckDeletedMetaData(){
        System.out.println("Mulai check deleted file : ");
        if (beforeAllFileMetaData==null){
            //Check metaData pertama kali, hanya inisialisasi saja
            beforeAllFileMetaData = Utility.getMetaDataFromFolder(clientDirectory);
        }else{
            //Buat metaData yang baru
            
            ArrayList<MetaData> currentMetaData = Utility.getMetaDataFromFolder(clientDirectory);
            
            System.out.println("Before MetaData :");
            Utility.PerformArrayListMetaData(beforeAllFileMetaData);
            System.out.println("Selesai print before");
            System.out.println("Current MetaData");
            Utility.PerformArrayListMetaData(currentMetaData);
            System.out.println("Selesai print current");
            
            //Check dengan folder lama, file yang tidak ada di folder baru dimasukkan ke deletedlist(timpa)
            boolean fileNotFound;
            int i,j;
            for(i=0;i<beforeAllFileMetaData.size();++i){
                fileNotFound = true;
                for(j=0;j<currentMetaData.size();++j){
                    if (beforeAllFileMetaData.get(i).getFilePath().equals(currentMetaData.get(j).getFilePath())){
                        fileNotFound = false;
                    }
                }
                if (fileNotFound){
                    //Rewrite ke list of deleted file
                    InsertRewriteOneMetaData(beforeAllFileMetaData.get(i), allDeletedFileMetaData);
                }
            }
            beforeAllFileMetaData = copyDeepArrListMetaData(currentMetaData);
            
            //Check deletedlist, jika file yang dideleted ternyata ada di folder yang baru,dihilangkan dari list
            for(i=0;i<allDeletedFileMetaData.size();++i){
                for(j=0;j<currentMetaData.size();++j){
                    if (allDeletedFileMetaData.get(i).getFilePath().equals(currentMetaData.get(j).getFilePath())){
                        //Delete dari list meta data sekarang
                        allDeletedFileMetaData.remove(i);
                        --i;
                        break;
                    }
                }
            }
            
        }
        System.out.println("List dari file yang terdelete : ");
        Utility.PerformArrayListMetaData(allDeletedFileMetaData);
        System.out.println("Selesai print");
    }
    
    public 
    
    Thread synchronizeThread = new Thread(){
        @Override
        public void run(){
            System.out.println("Thread start");
            
            while (true){
                
                //Bagian pengecekan folder client yang didelete
                if (isApplicationStart){
                    System.out.println("Cek data non login");
                    //Check file-file yang sudah didelete    
                    long lastTimeDeletedFileChecked = System.currentTimeMillis();
                    CheckDeletedMetaData();
                    while (System.currentTimeMillis()-lastTimeDeletedFileChecked<SYNCHRONIZE_TIME_DELETED){
                    }
                }
                System.out.println("isStillLogin : " + isStillLogin);
                System.out.println("isStillConnect : " + isStillConnect);
                
                long  lastTimeSync = System.currentTimeMillis();
                if(isStillLogin) System.out.println("LOGIN");
                while (isStillLogin && isStillConnect){
//                    System.out.println("Masuk isStillLogin");
                    if (System.currentTimeMillis()-lastTimeSync > Client.SYNCHRONIZE_TIME){
//                    System.out.println("Masuk isStillLogin");
                        //Bagian pengecekan folder client yang didelete
                        if (isApplicationStart){
                            System.out.println("Cek data login");
                            //Check file-file yang sudah didelete    
                            CheckDeletedMetaData();
                            
                            if (allDeletedFileMetaData.size()>0){
                                //Mulai sending list file yang dideleted ke server
                                try {
                                    System.out.println("kirim message delete");
                                    SendDeleteToServer();
                                } catch (IOException ex) {
                                    isStillConnect = false;
                                    System.out.println("SendDeleteToServer() failed");
                                }
                                catch (NoSuchAlgorithmException ex) {
                                    isStillConnect = false;
                                    System.out.println("SendDeleteToServer() failed");
                                }

                                int num = allDeletedFileMetaData.size();
                                System.out.println("Berhasil kirim delete, num = " + num);
                                try {
                                    byte[] b = new byte[1024];
                                    String strnum = Integer.toString(num);
                                    System.out.println("Strnum : " + strnum);
                                    bosSocket.write(strnum.getBytes());
                                    bosSocket.flush();
                                } catch (IOException ex) {
                                    isStillConnect = false;
                                    System.out.println("bosSocket.write(strnum.getBytes()) failed");
                                }

                                System.out.println("EAAA");
                                boolean getACK = false;
                                while (!getACK && isStillLogin && isStillConnect) {
                                    byte[] b = new byte[10];
                                    try {
                                        bisSocket.read(b);
                                    } catch (IOException ex) {
                                        isStillConnect = false;
                                        System.out.println("bisSocket.read(b) failed");
                                    }
                                    System.out.println("baca = " + Message.byteToString(b));

                                    try {
                                        if (Integer.parseInt(Message.byteToString(b).trim()) == Message.MESSAGE_ACK) {
                                            getACK = true;
                                        }
                                    } catch (NumberFormatException e) {
                                        isStillConnect = false;
                                        System.out.println("Salah parser int");
                                    }


                                }

                                for(int i=0;i<allDeletedFileMetaData.size();++i){
                                    String toSend = MetaData.getMetaDataString(allDeletedFileMetaData.get(i));
                                    try {
                                        bosSocket.write(toSend.getBytes());
                                        bosSocket.flush();
                                    } catch (IOException ex) {
                                        isStillConnect = false;
                                        System.out.println("bosSocket.write(toSend.getBytes()) failed");
                                    }

                                    System.out.println("Terima ack untuk server di for");
                                    getACK = false;
                                    while (!getACK && isStillLogin && isStillConnect) {
                                        byte[] b = new byte[10];
                                        try {
                                            bisSocket.read(b);
                                        } catch (IOException ex) {
                                            isStillConnect = false;
                                            System.out.println("bisSocket.read(b) failed");
                                        }
                                        System.out.println("baca = " + Message.byteToString(b));
                                        if (Integer.parseInt(Message.byteToString(b).trim()) == Message.MESSAGE_ACK) {
                                            getACK = true;
                                        }
                                    }
                                }
                                //Hapus semua anggota allDeletedFileMetaData
                                allDeletedFileMetaData.clear();
                            }
                            
                            System.out.println("Selesai delete login");
                        }
                        System.out.println("Mulai request metadata");
                        
                        ArrayList<MetaData> serverArrMetaData = new ArrayList<MetaData>();
                        
                        try {
                            System.out.println("Thread kirim ulang");
                            SendRequestMetaData();
                        } catch (UnsupportedEncodingException ex) {
//                            InitClientSocket();
//                            InitOutIn();
                            System.out.println("SendRequestMetaData() failed");
                            isStillConnect = false;
                        } catch (NoSuchAlgorithmException ex) {
//                            InitClientSocket();
//                            InitOutIn();
                            isStillConnect = false;
                            System.out.println("SendRequestMetaData() failed");
                        } catch (IOException ex) {
//                            InitClientSocket();
//                            InitOutIn();
                            isStillConnect = false;
                            System.out.println("SendRequestMetaData() failed");
                        }
                        
                        int i,num = 0;
                        try {
                            byte[] b = new byte[1024];
                            bisSocket.read(b);
                            num = Integer.parseInt(Message.byteToString(b).trim());
                            System.out.println("Request metadata num : " + num);
                        } catch (IOException ex) {
//                            InitClientSocket();
//                            InitOutIn();
                            System.out.println("isStillConnect=false");
                            isStillConnect = false;
                            System.out.println("Gagal baca angka pertama");
                        }
                        System.out.println("Baca " + num + " metadata.");
                        
                        //Sending ack ke server
                        try {
                            SendAckToServer();
                        } catch (UnsupportedEncodingException ex) {
//                            InitClientSocket();
//                            InitOutIn();
                            System.out.println("SendAckToServer() failed()");
                            isStillConnect = false;
                        } catch (IOException ex) {
//                            InitClientSocket();
//                            InitOutIn();
                            System.out.println("SendAckToServer() failed()");
                            isStillConnect = false;
                        } catch (NoSuchAlgorithmException ex) {
//                            InitClientSocket();
//                            InitOutIn();
                            System.out.println("SendAckToServer() failed()");
                            isStillConnect = false;
                        }

                        for(i=0;i<num;++i){
                            byte[] b = new byte[1000];
                            try {
                                bisSocket.read(b);
                            } catch (IOException ex) {
//                                InitClientSocket();
//                                InitOutIn();
                                System.out.println("isStillConnect=false");
                                isStillConnect = false;
                                System.err.println("Error in getting metadata file");
                                //Kasus data metadata rusak
                                serverArrMetaData.clear();
                                break;
                            }
                            String metaDataString = Message.byteToString(b);
//                            System.out.println("Meta-data ke-" + i + " : " + metaDataString);
                            
                            serverArrMetaData.add(MetaData.readMetaDataString(metaDataString));

                            //Sending ack ke server
                            try {
                                SendAckToServer();
                            } catch (UnsupportedEncodingException ex) {
//                                InitClientSocket();
//                                InitOutIn();
                                System.out.println("SendAckToServer() failed()");
                                isStillConnect = false;
                                //Kasus data metadata rusak
                                serverArrMetaData.clear();
                                break;
                            } catch (IOException ex) {
//                                InitClientSocket();
//                                InitOutIn();
                                System.out.println("SendAckToServer() failed()");
                                isStillConnect = false;
                                //Kasus data metadata rusak
                                serverArrMetaData.clear();
                                break;
                            } catch (NoSuchAlgorithmException ex) {
//                                InitClientSocket();
//                                InitOutIn();
                                System.out.println("SendAckToServer() failed()");
                                isStillConnect = false;
                                //Kasus data metadata rusak
                                serverArrMetaData.clear();
                                break;
                            }
                        }
                        lastTimeSync = System.currentTimeMillis();

                        FinalSynchronize(serverArrMetaData);
                    }
                    
                    if (!isStillConnect && isStillLogin){
                        System.out.println("Koneksi putus, coba ulang koneksi");
                        InitClientSocket();
                        InitOutIn();
                        try {
//                            Login("novan", "12345678");
                            Login(username, password);
                            isStillLogin = true;
                            byte[] b = new byte[1000];
                            bisSocket.read(b);
                            String result = Message.byteToString(b);
                            System.out.println("Dari server : " + result);
                        } catch (UnsupportedEncodingException ex) {
                            System.out.println("Login dari thread failed()");
                        } catch (NoSuchAlgorithmException ex) {
                            System.out.println("Login dari thread failed()");
                        } catch (IOException ex) {
                            System.out.println("Login dari thread failed()");
                        }
                    }
                    
                }
            }
        }
    };
    
//    public Thread DeletedFileManagerThread = new Thread(){
//        @Override
//        public void run(){
//            while (true){
//                if (!clientDirectory.equals("")){
//                    File deletedFileList = new File(DELETED_FILE_NAME);
//                    if (deletedFileList.exists()){
//                        
//                    }else{
//                        
//                    }
//                }
//            }
//        }
//    };
    
    /**
     * Sinkronisasi final antara file client dengan server,
     * dimulai dari client terlebih dahulu
     * @param serverArrMetaData 
     */
    public void FinalSynchronize(ArrayList<MetaData> serverArrMetaData){
        arrMetaData = CreateClientMetadata();
        int i;
//        isStillLogin = false;
        System.out.println("size server="+serverArrMetaData.size());
        for(i=0;i<arrMetaData.size();++i){
            int j=0;
            boolean found = false;
            while(j<serverArrMetaData.size()){
            //for(j=0;j<serverArrMetaData.size();++j){
                if(arrMetaData.get(i).getFilePath().equals(serverArrMetaData.get(j).getFilePath())){
                    System.out.println("MetaData Client sama "+arrMetaData.get(i).getFilePath());
                    
                    if (!arrMetaData.get(i).getFileChecksum().equals(serverArrMetaData.get(j).getFileChecksum())){
                        System.out.println("Checksum beda");
                        System.out.println("client cheksum : " + arrMetaData.get(i).getFileChecksum());
                        System.out.println("server checksum : " + serverArrMetaData.get(j).getFileChecksum());
                        if (arrMetaData.get(i).getLastModified()>serverArrMetaData.get(j).getLastModified()){
                            uploadingProcess(arrMetaData.get(i));
                        }else{
                            downloadingProcess(serverArrMetaData.get(j));
                        }
                    }
                    
                    serverArrMetaData.remove(j);
                    //j+=1;
                    found = true;
                }else j+=1;
            }
            if(!found){
                uploadingProcess(arrMetaData.get(i));
            }
        }
        System.out.println("size server become="+serverArrMetaData.size());
        for(i=0;i<serverArrMetaData.size();i++){
            System.out.println("downloading metadata "+(i+1) + ":"+serverArrMetaData.get(i).getFilePath());
            if (serverArrMetaData.get(i).getFileType()==Utility.TYPE_FOLDER){
                Utility.CreateNewDirectory(clientDirectory+"\\"+serverArrMetaData.get(i).getFilePath());
                System.out.println("Directory created in "+clientDirectory+"\\"+serverArrMetaData.get(i).getFilePath());
            }
            else{
                System.out.println("SEND FILE");
                downloadingProcess(serverArrMetaData.get(i));
            }
        }
    }
    public void downloadingProcess(MetaData metaData){
        try {
            DownloadRequest(metaData.getFileSize(),metaData.getFilePath()); // f1/file2
            System.out.println("meminta port");
            byte[] b = new byte[1000];
            bisSocket.read(b);
            String port = Message.byteToString(b).trim();
            int portNumber = Integer.parseInt(port);
            System.out.println("port didapat "+portNumber);
            DownloadRequestNewPort(Integer.parseInt(port), metaData.getFileSize(),metaData.getFilePath());
        } catch (UnsupportedEncodingException ex) {
            System.out.println("Encoding error");
            isStillConnect = false;
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("no such algoritma");
            isStillConnect = false;
        } catch (IOException ex) {
            System.out.println("io exception");
            isStillConnect = false;
        }
    }
    public void uploadingProcess(MetaData metaData){
        try {
            System.out.println("uploading "+metaData.getFilePath());
            MessageUpload(metaData.getFileSize(), metaData.getFileType(), metaData.getFilePath());
            System.out.println("meminta port");
            byte[] b = new byte[1000];
            bisSocket.read(b);
            String port = Message.byteToString(b).trim();
            int portNumber = Integer.parseInt(port);
            System.out.println("port didapat "+portNumber);
            Uploading(portNumber, metaData.getFilePath());
        } catch (UnsupportedEncodingException ex) {
            System.out.println("Encoding error");
            isStillConnect = false;
        } catch (NoSuchAlgorithmException ex) {
            System.out.println("no such algoritma");
            isStillConnect = false;
        } catch (IOException ex) {
            System.out.println("io exception");
            isStillConnect = false;
        }
        
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException, NoSuchAlgorithmException {
        // TODO code application logic here
        Client client = new Client();
//        String inputString;
        
//        while ((inputString=client.inClient.readLine())!=null){
//            System.out.println("Dari server : " + inputString);
//            client.outClient.println("ADUH");
//        }
        
        
//        while ((inputString=client.inClient.readLine())!=null){
//            System.out.println(inputString);
//        }
        
//        while ((inputString=client.inClient.readLine())!=null){
//            System.out.println("Nilai masukan : " + inputString);
//        }
        
        client.Login("novan", "1234");
        
//        client.setClientDirectory("D:\\");
//        client.CreateNewClientDirectory();
        
//        client.DownloadRequest(5676688,"hasil.avi"); // f1/file2


        client.Logout();
        
    }
}

